package com.UdpRecieve.tool;


import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*rar解压压缩*/
public class FileRar {

    /**
     *解压rar
     * @param inDir
     * @param outDir
     * @throws Exception
     */
    public static void unRar(String inDir, String outDir) throws Exception {
        File outFileDir = new File(outDir);
        File inFileDir = new File(inDir);
        if(!inFileDir.exists()){
            throw new Exception("文件不存在");
        }
        if (!outFileDir.exists()) {
            boolean isMakDir = outFileDir.mkdirs();
            if (isMakDir) {
                System.out.println("创建压缩目录成功");
            }
        }
        Archive archive = new Archive(new FileInputStream(inFileDir));
        FileHeader fileHeader = archive.nextFileHeader();
        while (fileHeader != null) {
            if (fileHeader.isDirectory()) {
                fileHeader = archive.nextFileHeader();
                continue;
            }
            String outFileNameString = fileHeader.getFileNameW();
            if(!existZH(outFileNameString)){
                outFileNameString = fileHeader.getFileNameString();
            }
            File out = new File(outDir+"\\" + outFileNameString);//产生乱码
            if (!out.exists()) {
                if (!out.getParentFile().exists()) {
                    out.getParentFile().mkdirs();
                }
                out.createNewFile();
            }
            FileOutputStream os = new FileOutputStream(out);
            archive.extractFile(fileHeader, os);

            os.close();

            fileHeader = archive.nextFileHeader();
        }
        archive.close();
    }
    public static boolean existZH(String str) {
        String regEx = "[\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        while (m.find()) {
            return true;
        }
        return false;
    }


    /*public static boolean unRarFile(String sourceRarPath, String destRarPath){
        Archive archive = null;
        FileOutputStream fos = null;
        try {
            File sourceRar = new File(sourceRarPath);
            File destDir = new File(destRarPath);
            archive = new Archive(sourceRar);
            FileHeader fh = archive.nextFileHeader();
            File destFileName = null;
            System.out.println("Uncompressing:" + sourceRar.getName() + "...");
            while (fh != null) {
                String compressFileName = fh.getFileNameString().trim();
                destFileName = new File(destDir.getAbsolutePath() + "/" + compressFileName);
                if (fh.isDirectory()) {
                    if (!destFileName.exists()) {
                        destFileName.mkdirs();
                    }
                    fh = archive.nextFileHeader();
                    continue;
                }
                if (!destFileName.getParentFile().exists()) {
                    destFileName.getParentFile().mkdirs();
                }
                fos = new FileOutputStream(destFileName);
                archive.extractFile(fh, fos);
                fos.close();
                fos = null;
                fh = archive.nextFileHeader();
            }

            archive.close();
            archive = null;
            System.out.println("Uncompress:" + sourceRar.getName() + " end!");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (archive != null) {
                try {
                    archive.close();
                    archive = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }*/


    public static void main(String[] args) {
        getUnZipAnnRarTest();
    }
    public static void getUnZipAnnRarTest() {
        String inFilePath = "C:\\Users\\Administrator\\Desktop\\目标目录.rar";
        String outFilePath = "C:\\Users\\Administrator\\Desktop\\";
        File fileOut = new File(outFilePath);
        try {
            if(!fileOut.exists()){
                fileOut.mkdirs();
            }
            FileRar.unRar(inFilePath,outFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
